package rise.message;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public abstract class Packet {

	private PacketType packetType;
	private int port;
	private double lat, lon;
	
	static final String CONNECTION_TAG = "connection";
	static final String LAT_ATTRIBUTE = "lat";
	static final String LON_ATTRIBUTE = "lon";
	static final String PORT_ATTRIBUTE = "port";
	
	public static final int OK = 0;
	public static final int UNKNOWN_PACKET = -1;
	public static final int CONNECTION_ERROR = -2;
	
	public Packet(PacketType packetType, int port, double lat, double lon){
		this.packetType = packetType;
		this.port = port;
		this.lat = lat;
		this.lon = lon;
	}
	
	/*
	public Packet(PacketType packetType, int port){
		this(packetType, port, 0.0, 0.0);
	}*/
	
	public abstract String toXML();
	
	Element createConnectionElement(Document doc){
		Element connectionElement = doc.createElement(Packet.CONNECTION_TAG);
		connectionElement.setAttribute(Packet.PORT_ATTRIBUTE, Integer.toString(port));
		connectionElement.setAttribute(Packet.LAT_ATTRIBUTE, Double.toString(lat));
		connectionElement.setAttribute(Packet.LON_ATTRIBUTE, Double.toString(lon));
		
		return connectionElement;
	}
	
	abstract String rootElementTag();

	String generateString(Document doc) throws TransformerFactoryConfigurationError, TransformerException{
	    DOMSource domSource = new DOMSource(doc);
	    
	    Transformer transformer = TransformerFactory.newInstance().newTransformer();
	    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
	    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
	    
	    StringWriter sw = new StringWriter();
	    StreamResult sr = new StreamResult(sw);
	    transformer.transform(domSource, sr);

	    return sw.toString();
	}
	
	Document createXMLDocument() throws ParserConfigurationException{
	    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder builder = factory.newDocumentBuilder();
	    DOMImplementation impl = builder.getDOMImplementation();
	    Document doc = impl.createDocument(null, null, null);
	    return doc;
	}
	
	public static Document createXMLDocumentFromString(String xmlString){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		InputSource source = new InputSource(new StringReader(xmlString));
		Document doc = null;
		try {
			doc = factory.newDocumentBuilder().parse(source);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	/*public String getAddress() {
		return address;
	}*/
	
	public int getPort() {
		return port;
	}
	
	public PacketType getPacketType() {
		return packetType;
	}
	
	public boolean isMessage(){
		return false;
	}
	
	public double getLat() {
		return lat;
	}
	
	public double getLon() {
		return lon;
	}
	
	public void setLat(double lat) {
		this.lat = lat;
	}
	
	public void setLon(double lon) {
		this.lon = lon;
	}
	
	public abstract String getReceiver();
	
	/**
	 * TODO: Write to file
	 * public File createFile(Document doc)
	 * **/
}